package com.fiap.phaseListener;

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import com.fiap.mb.UsuarioMB;



public class SitePhaseListener implements PhaseListener {

	private static final long serialVersionUID = 1L;

	@Override
	public void afterPhase(PhaseEvent arg0) {
		
		
	}

	@Override
	public void beforePhase(PhaseEvent event) {
		FacesContext context = event.getFacesContext();
		String browser = (String) context.getExternalContext().getRequestHeaderMap().get("User-Agent").toLowerCase();
		NavigationHandler handler = context.getApplication().getNavigationHandler();
		
		if(!context.getViewRoot().getViewId().contains("login") || !context.getViewRoot().getViewId().contains("loginMobile")){
			
			UsuarioMB usuario = (UsuarioMB) context.getExternalContext().getSessionMap().get("usuarioMB");
			
			if(usuario == null) { 
				handler.handleNavigation(context, null, "login"); 
			} else if(usuario.getUsuarioTO().isAutenticado()) {
				return;
			} else {
				
				handler.handleNavigation(context, null, "login");
			}
		}
		
		if(browser.contains("avantgo") || browser.contains("bolt") || browser.contains("docomo") || browser.contains("up.browser") || browser.contains("vodafone") 
				|| browser.contains("j-phone") || browser.contains("ddipocket") || browser.contains("pdxgw") || browser.contains("astel") || browser.contains("android")
				|| browser.contains("eudoraweb") || browser.contains("ppc") || browser.contains("minimo") || browser.contains("plink") || browser.contains("plucker") 
				|| browser.contains("netfront") || browser.contains("wm5 pie") || browser.contains("xiino") || browser.contains("tablet") || browser.contains("ipad")
				|| browser.contains("iphone") || browser.contains("itunes") || browser.contains("vendorid") || browser.contains("wap") || browser.contains("bb10") 
				|| browser.contains("blackberry") || browser.contains("opera mini") || browser.contains("cricket") || browser.contains("iemobile") 
				|| browser.contains("wosbrowser") || browser.contains("windows phone") || browser.contains("htc") || browser.contains("xv6850") 
				|| browser.contains("kindle") || browser.contains("teleca") || browser.contains("mib/") || browser.contains("portalmmm") || browser.contains("nintendo") 
				|| browser.contains("mobile") || browser.contains("nokia") || browser.contains("symbian") || browser.contains("opera mobi") || browser.contains("fennec") 
				|| browser.contains("tear") || browser.contains("midori") || browser.contains("prism") || browser.contains("smartphone") || browser.contains("webos") 
				|| browser.contains("palm") || browser.contains("blazer") || browser.contains("palmsource") || browser.contains("mobileexplorer") 
				|| browser.contains("regking") || browser.contains("epoc") || browser.contains("samsung-gt-s3653w") || browser.contains("samsung-gt-s5620")
				|| browser.contains("samsung-s8003") || browser.contains("bada") || browser.contains("samsung-sphm800") || browser.contains("sec-sghe600")
				|| browser.contains("sec-sgh600") || browser.contains("sec-sghd410") || browser.contains("j2me") || browser.contains("reqwirelessweb/3.2 s55")
				|| browser.contains("sonyericssonk800i") || browser.contains("sonyericssonk608i") || browser.contains("semc-browser") 
				|| browser.contains("sonyericssont200") || browser.contains("sonyericssonp800") || browser.contains("sonyericssonp900") 
				|| browser.contains("sonyericssont610")	|| browser.contains("playstation") || browser.contains("ucweb") || browser.contains("wp7")){
			
			handler.handleNavigation(context, null, "loginMobile");
		}
		else{
			handler.handleNavigation(context, null, "login");
		}
		
	}

	@Override
	public PhaseId getPhaseId() {
		return PhaseId.RENDER_RESPONSE;
	}

}
